﻿/// <reference path="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1-vsdoc.js" />
var lightbox$overlay = null;
var lightbox$frame = null;
var lightbox$title = null;
var lightbox$image = null;
var lightbox$wait = null;
var lightbox$next = null;
var lightbox$prev = null;
var lightbox$pics = [];
var lightbox$current = null;

function lightbox$run()
{
    $('a[rel^=lightbox]')
        .click(lighbox$click);

    lightbox$overlay = $('<div id="jslightbox" />')
        .appendTo(document.body)
        .hide();

    lightbox$frame = $('<div id="jslightboxframe" />')
        .appendTo(document.body)
        .hide();

    lightbox$title = $('<div id="jslightboxtitle" />')
        .appendTo(lightbox$frame)

    lightbox$image = $('<img id="jslightboximage" />')
        .appendTo(lightbox$frame)
        .click(lightbox$hide);

    lightbox$wait = $('<img id="jslightboxwait" src="images/wait.gif" width="32" height="32" />')
        .appendTo(lightbox$frame)
        .hide();

    lightbox$next = $('<img id="jslightboxnext" src="images/next_button.png" width="100" height="100" />')
        .appendTo(lightbox$overlay)
        .click(lightbox$movenext);

    lightbox$prev = $('<img id="jslightboxprev" src="images/prev_button.png" width="100" height="100" />')
        .appendTo(lightbox$overlay)
        .click(lightbox$moveprev);

    $(window).resize(lightbox$resize);
}

function lighbox$click(item)
{
    item.preventDefault();

    var rel = $(this).attr('rel');
    var href = $(this).attr('href');
    var rx = /^lightbox(\[(.*)\])?$/i;
    var collection = rx.exec(rel)[2];

    lightbox$pics = [];


    $('a[rel=' + rel + ']').each(function (i)
    {
        var pic = {
            uri: $(this).attr('href'),
            title: $(this).attr('title'),
            text: $(this).text()
        };

        if (pic.uri == href)
            lightbox$current = i;

        lightbox$pics.push(pic);
    });

    lightbox$show();
}

function lightbox$resize()
{
    lightbox$image
        .css('max-width', lightbox$overlay.width() - 100)
        .css('max-height', lightbox$overlay.height() - 100);
    lightbox$frame
        .css("top", (lightbox$overlay.height() - lightbox$frame.height()) / 2)
        .css("left", (lightbox$overlay.width() - lightbox$frame.width()) / 2);
    lightbox$prev
        .css("top", (lightbox$overlay.height() - lightbox$prev.height()) / 2);
    lightbox$next
        .css("top", (lightbox$overlay.height() - lightbox$next.height()) / 2);
}

function lightbox$show()
{
    lightbox$overlay.show();
    lightbox$frame.show();
    lightbox$load(lightbox$current);
    lightbox$setVisibility();
    lightbox$resize();
}

function lightbox$hide()
{
    lightbox$overlay.hide();
    lightbox$frame.hide();
}

function lightbox$load(index)
{
    var pic = lightbox$pics[index];

    lightbox$image.hide();
    lightbox$title.hide();
    lightbox$wait.show();
    lightbox$title.text(pic.title);
    lightbox$resize();

    setTimeout(function () { lightbox$image.attr('src', pic.uri).load(lightbox$loaded); }, 300);
}

function lightbox$loaded()
{
    lightbox$image.show();
    lightbox$wait.hide();
    if (lightbox$title.text() != '' && lightbox$title.text() != null)
        lightbox$title.show();
    lightbox$resize();
}

function lightbox$setVisibility()
{
    if (lightbox$current == 0)
        lightbox$prev.hide();
    else
        lightbox$prev.show();

    if (lightbox$current == lightbox$pics.length - 1)
        lightbox$next.hide();
    else
        lightbox$next.show();
}

function lightbox$moveprev()
{
    if (lightbox$current > 0)
    {
        lightbox$current--;
        lightbox$load(lightbox$current);
        lightbox$setVisibility();
    }
}

function lightbox$movenext()
{
    if (lightbox$current < lightbox$pics.length - 1)
    {
        lightbox$current++;
        lightbox$load(lightbox$current);
        lightbox$setVisibility();
    }
}